/************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     pic.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-01
  * @brief    
 *************************************************************************/

#include "pit.h"
#include <string.h>
#include "function_drv.h"
#include "tp_ctp.h"
#include "bsp.h"
#include "wav.h"
#include "uart.h"
#include "lt168_pit.h"
#include "tftlcd.h"	//QSPI_Vincent
#include "lt168_eic.h"	//QSPI_Vincent
#include "rtc.h"
//zyy----------------------------
#include "gif.h"
#include "analogClock.h"
#include "digitalClock.h"
#include "scrollText.h"
#include "uiCounter.h"
#include "varCounter.h"
#include "needle.h"
#include "gesture.h"
#include "button.h"
#include "varButton.h"
#include "sliderBar.h"
#include "circularTouch.h"
#include "slideMenu.h"
#include "keyBoard.h"
#include "multiVarButton.h"
#include "uiCounter.h"
#include "popupBox.h"
#include "curve.h"
#include "uiQRCode.h"
#include "uiEncoder.h"
#include "uiIcon.h"
//--------------------------------

uint8_t flag_MB_REG[120];

#if (UARTBUS_OPTION == 0 || UARTBUS_OPTION == 3 || UARTBUS_OPTION == 4 ||  \
	UARTBUS_OPTION == 5 || UARTBUS_OPTION == 6)

// timer for dynamic display
void PIT1_Init(void)
{
	//PRE 0=1 1=2 2=4 3=8 4=16 5=32 6=64 7=128 ......
	//timeout period  = PRE * (PM + 1) * clocks

	PIT1->PCSR=0x0;//PIT3->U16PCSR.EN=0;//清零EN位
	PIT1->U16_PCSR.PRE = 5;         //预分频
	PIT1->PMR = 10*(SYS_CLOCK/1000)/32;//（168MHZ） 10ms timer
	PIT1->U16_PCSR.OVW = 1;
	PIT1->U16_PCSR.PIE = 1;//中断使能PIE
	PIT1->U16_PCSR.RLD = 1;//重装位RLD
	PIT1->U16_PCSR.EN  = 1;//PIT使能EN

	EIC->IER |=IRQ(10); //enable pit3 interrupt
}


void PIT1_Handler(void)
{
	if(fanhui_flag)
	{
		fanhui_timesum++;
		if(fanhui_timesum==100)//1s
		{
		  if(pic_id==4)
		  {
		  Display_page(3);
		  fanhui_flag=0;
		  }
		  if(pic_id==5)
		  {
		  Display_page(7);
		  fanhui_flag=0;
		  }
		}
	 }
	if(min_flag)
	 {
		 minA_timesum++;
		 if(minA_timesum==1000)//10S
		 {
		 Display_page(1);
		 min_flag=0;
		 }
	 }
	//LTPrintf("minA_timesum = %d \r\n",minA_timesum);
	PIT1->U16_PCSR.PIF = 1;		//clear PIF interrupt flag
	css_res_time ++;
	if (Var_Count_num > 0)
		Var_Count_flag = 1;
	if(Pointer_Count_num >0)
		Pointer_Count_flag = 1;
	if (Gif_num > 0)
		Gif_flag = 1;
	if (Adj_loop > 0)
		Adj_time++;
	if (DioKb_flag == 1)
		Cusor_time++;
	if (AioKb_flag == 1)
		Cusor_time++;
	if (GBKioKb_flag == 1)
		Cusor_time++;
	if (Scroll_num > 0)
		Scroll_flag = 1;
	if (curve_num > 0)
		curve_flag = 1;
	if (Backlight_flag)
		Backlight_count++;
	if (buzzer_start)
	{
		buzzer_10ms_count++;
		if (buzzer_10ms_count >= 5)
		{
			touch_buzzer_close();
			buzzer_start = 0;
			buzzer_10ms_count = 0;
		}
	}

#if (LT_TOUCH_FLAG == 1)
	if (RTP_read_Flag == 1)
	{
		RTP_time++;
		if (RTP_time > 5)
		{
			RTP_read_Flag = 0;
			RTP_time = 0;
		}
	}
#endif
	page_refreshing++;
	if(ctp_press_t < 0xFF) ctp_press_t++;

#if LT_ENCODER
	if (SW_flag4 != 2 || SW_flag4 != 3)
		key_time++;

	if (Encoder_read_Flag == 1)
	{
		Backlight_count = 0;
		if (Encoder_type == 0)
		{
			Encoder_time++;
			if (Encoder_time > 1)
			{
				LTPrintf("DDDDDDDDD\r\n");
				if (Read_Encoder_key == 1)
				{
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
					SW_flag = 1;
				}
			}
		}
		else if (Encoder_type == 4)
		{
			Encoder_time++;
			if (Encoder_time > 1)
			{
				if (SW_flag4 == 1)
				{
					SW_flag4 = 2;
					SW_flag = 3;
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
				}
				if (Encoder_time > (Long_press * 12) && SW_flag4 != 3)//5
				{
					//LTPrintf("DDDDDDDDD\r\n");
					SW_flag4 = 3;
					SW_flag = 4;
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
				}
				if (Read_Encoder_key == 1)
				{
					SW_flag4 = 1;
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
				}
			}
		}
	}
	if (SW_flag4 == 1)
	{
		Encoder_time++;
		if (Encoder_time > Double_click_interval * 12) // Double-click valid time exceeded//5
		{
			SW_flag4 = 1;
			SW_flag = 2;
		}
	}
	if (Enc1)
	{
		Encoder_Freetime++;
		if (Encoder_Freetime > 100)
		{
			Encoder_Freetime = 0;
			knob_dir = 0;
			Enc1 = 0;
		}
	}
#endif
}
#endif


volatile uint8_t Addr_ModBus_Sla = 0x01;

#if (UARTBUS_OPTION == 1)

#define MB_ERR_CMD 0x01
#define MB_ERR_ADDR 0x02
#define MB_ERR_DATA 0x03
#define MB_ERR_FAULT 0x04

MB_CMD_RXINFO gMB_CMDRX;
uint8_t MB_SLA_RXBUF[256] = {0};
uint16_t MB_SLA_Len = 0;
uint8_t Feedback_ModbusBuf[256] = {0};

 uint32_t pit_1ms_count = 0;
 uint8_t uart_flag = 0;
 uint32_t pit_1ms_count2 = 0;
 void PIT1_Init(void)
 {
 	//PRE 0=1 1=2 2=4 3=8 4=16 5=32 6=64 7=128 ......
 	//timeout period  = PRE * (PM + 1) * clocks

 	PIT1->PCSR=0x0;//PIT3->U16PCSR.EN=0;//清零EN位
 	PIT1->U16_PCSR.PRE = 5;         //预分频
 	PIT1->PMR = 1*(SYS_CLOCK/1000)/32;//（168MHZ） 10ms timer
 	PIT1->U16_PCSR.OVW = 1;
 	PIT1->U16_PCSR.PIE = 1;//中断使能PIE
 	PIT1->U16_PCSR.RLD = 1;//重装位RLD
 	PIT1->U16_PCSR.EN  = 1;//PIT使能EN

 	EIC->IER |=IRQ(10); //enable pit3 interrupt
 }
 void PIT1_Handler(void)
 {
	 uint16_t CRC_MCU = 0, CRC_RX = 0;
	uint16_t i = 0, j = 0;
	uint8_t num = 0, cnt = 0;
	uint16_t temp = 0;

	uint16_t k;

 	PIT1->U16_PCSR.PIF = 1;		//clear PIF interrupt flag

	pit_1ms_count++;
	pit_1ms_count2++;
	// If the serial port is detected for 5ms and no data is received, the reception is completed
	if (pit_1ms_count == 10 && uart_flag)
	{
		pit_1ms_count = 0;
		uart_flag = 0;

		MB_SLA_Len = gUsartRx.Count;
		memcpy(MB_SLA_RXBUF, gUsartRx.Buf, MB_SLA_Len);
		gUsartRx.Count = 0;

		if (MB_SLA_RXBUF[0] == Addr_ModBus_Sla || MB_SLA_RXBUF[0] == 0) // Transceiver node consistency or broadcast mode
		{
			CRC_MCU = (MB_SLA_RXBUF[MB_SLA_Len - 1] << 8) + (MB_SLA_RXBUF[MB_SLA_Len - 2] & 0x00FF);
			CRC_RX = CRC16(MB_SLA_RXBUF, MB_SLA_Len - 2);
			if (CRC_MCU == CRC_RX) // CRC check is correct
			{
				gMB_CMDRX.CMD = MB_SLA_RXBUF[1];
				gMB_CMDRX.Addr = MB_SLA_RXBUF[2] << 8 | MB_SLA_RXBUF[3];

				Var_AddrRemapping(&gMB_CMDRX.Addr);

				if (gMB_CMDRX.CMD == 0x03 && MB_SLA_RXBUF[0] != 0) // Read command response (broadcast mode read function is meaningless)
				{
					gMB_CMDRX.Num = MB_SLA_RXBUF[4] << 8 | MB_SLA_RXBUF[5];
					if (gMB_CMDRX.Addr < VAR_ADDR_S) // Variable address
					{
						Feedback_ModbusBuf[0] = Addr_ModBus_Sla;
						Feedback_ModbusBuf[1] = gMB_CMDRX.CMD;
						Feedback_ModbusBuf[2] = 2 * gMB_CMDRX.Num;
						for (i = 0; i < gMB_CMDRX.Num; i++)
						{
							Feedback_ModbusBuf[3 + 2 * i] = var[2 * (gMB_CMDRX.Addr + i)];
							Feedback_ModbusBuf[4 + 2 * i] = var[2 * (gMB_CMDRX.Addr + i) + 1];
						}
						LT_SendData_CRC_NoFrame(Feedback_ModbusBuf, 3 + Feedback_ModbusBuf[2]);
					}
					else if (gMB_CMDRX.Addr >= VAR_ADDR_S && gMB_CMDRX.Addr <= VAR_ADDR_E) // Register address
					{
						if (gMB_CMDRX.Addr <= (VAR_TIME + 5) || gMB_CMDRX.Addr + gMB_CMDRX.Num >= (VAR_TIME + 0))
						{
							var[(VAR_TIME + 0) * 2 + 1] = calendar.w_year - 2000;
							var[(VAR_TIME + 1) * 2 + 1] = calendar.w_month;
							var[(VAR_TIME + 2) * 2 + 1] = calendar.w_date;
							var[(VAR_TIME + 3) * 2 + 1] = calendar.hour;
							var[(VAR_TIME + 4) * 2 + 1] = calendar.min;
							var[(VAR_TIME + 5) * 2 + 1] = calendar.sec;
						}

						Feedback_ModbusBuf[0] = Addr_ModBus_Sla;
						Feedback_ModbusBuf[1] = gMB_CMDRX.CMD;
						Feedback_ModbusBuf[2] = 2 * gMB_CMDRX.Num;
						for (i = 0; i < gMB_CMDRX.Num; i++)
						{
							Feedback_ModbusBuf[3 + 2 * i] = var[2 * (gMB_CMDRX.Addr + i)];
							Feedback_ModbusBuf[4 + 2 * i] = var[2 * (gMB_CMDRX.Addr + i) + 1];
						}
						LT_SendData_CRC_NoFrame(Feedback_ModbusBuf, 3 + Feedback_ModbusBuf[2]);
					}
					else
					{
						Feedback_ModbusBuf[0] = Addr_ModBus_Sla;
						Feedback_ModbusBuf[1] = gMB_CMDRX.CMD + 0x80;
						Feedback_ModbusBuf[2] = MB_ERR_ADDR;
						LT_SendData_CRC_NoFrame(Feedback_ModbusBuf, 3);
					}
				}
				else if (gMB_CMDRX.CMD == 0x06) // Write instruction response
				{
					if (gMB_CMDRX.Addr < VAR_ADDR_S) // Variable address
					{
						var[2 * gMB_CMDRX.Addr] = MB_SLA_RXBUF[4];
						var[2 * gMB_CMDRX.Addr + 1] = MB_SLA_RXBUF[5];

						if (MB_SLA_RXBUF[0] != 0)
							LT_SendData_CRC_NoFrame(MB_SLA_RXBUF, 6);
					}
					else if (gMB_CMDRX.Addr >= VAR_ADDR_S && gMB_CMDRX.Addr <= VAR_ADDR_E) // Register address
					{
						var[2 * gMB_CMDRX.Addr] = MB_SLA_RXBUF[4];
						var[2 * gMB_CMDRX.Addr + 1] = MB_SLA_RXBUF[5];
						flag_MB_REG[gMB_CMDRX.Addr - VAR_ADDR_S] = 1;

						if (MB_SLA_RXBUF[0] != 0)
							LT_SendData_CRC_NoFrame(MB_SLA_RXBUF, 6);
					}
					else
					{
						Feedback_ModbusBuf[0] = Addr_ModBus_Sla;
						Feedback_ModbusBuf[1] = gMB_CMDRX.CMD + 0x80;
						Feedback_ModbusBuf[2] = MB_ERR_ADDR;
						if (MB_SLA_RXBUF[0] != 0)
							LT_SendData_CRC_NoFrame(Feedback_ModbusBuf, 3);
					}
				}
				else if (gMB_CMDRX.CMD == 0x10) // Write multiple instruction response
				{
					gMB_CMDRX.Num = MB_SLA_RXBUF[4] << 8 | MB_SLA_RXBUF[5];
					if (gMB_CMDRX.Addr < VAR_ADDR_S) // Variable address
					{
						// Save data
						for (i = 0; i < gMB_CMDRX.Num; i++)
						{
							var[2 * (gMB_CMDRX.Addr + i)] = MB_SLA_RXBUF[7 + 2 * i];
							var[2 * (gMB_CMDRX.Addr + i) + 1] = MB_SLA_RXBUF[8 + 2 * i];
						}
						if (MB_SLA_RXBUF[0] != 0)
							LT_SendData_CRC_NoFrame(MB_SLA_RXBUF, 6);
					}
					else if (gMB_CMDRX.Addr >= VAR_ADDR_S && gMB_CMDRX.Addr <= VAR_ADDR_E) // Register address
					{
						for (i = 0; i < gMB_CMDRX.Num; i++)
						{
							var[2 * (gMB_CMDRX.Addr + i)] = MB_SLA_RXBUF[7 + 2 * i];
							var[2 * (gMB_CMDRX.Addr + i) + 1] = MB_SLA_RXBUF[8 + 2 * i];
							flag_MB_REG[gMB_CMDRX.Addr - VAR_ADDR_S + i] = 1;
						}
						if (MB_SLA_RXBUF[0] != 0)
							LT_SendData_CRC_NoFrame(MB_SLA_RXBUF, 6);
					}
					else if (gMB_CMDRX.Addr >= VAR_CURVE_WS && gMB_CMDRX.Addr <= VAR_CURVE_WE) 
					{
						gMB_CMDRX.Num = MB_SLA_RXBUF[4] << 8 | MB_SLA_RXBUF[5];

						temp = gMB_CMDRX.Addr & 0xFFF;
						for (i = 0; i < CURVE_SIZE; i++) //Record how many groups of channels
						{
							if (temp & (1 << i))
								num++;
						}

						i = 0;
						j = 0;
						while (j < gMB_CMDRX.Num) // Store data to corresponding channels respectively
						{
							if (temp & (1 << i))
							{
								Curve_buff[i][Curve_count[i]] = MB_SLA_RXBUF[7 + 2 * j] << 8 | MB_SLA_RXBUF[8 + 2 * j];
								Curve_count[i]++;
								if (Curve_count[i] >= 1024)
								{
									Curve_count[i] = 0;
								}
								curve_update_flag[i] = 1;
								j++;
							}
							i++;
							if (i >= CURVE_SIZE)
								i = 0;
						}
						/* A consensus was reached that when multi-channel buffering data was triggered,
						if some channel data was missing, the missing part would be supplemented */
						if (gMB_CMDRX.Num % num)
						{
							for (i = 0; i < CURVE_SIZE; i++)
							{
								if (temp & (1 << i))
									cnt++;
								if (cnt > gMB_CMDRX.Num % num && (temp & (1 << i)))
								{
									if (Curve_count[i] != 0)
										Curve_buff[i][Curve_count[i]] = Curve_buff[i][Curve_count[i] - 1];
									else
										Curve_buff[i][0] = Curve_buff[i][1023];

									Curve_count[i]++;
									if (Curve_count[i] >= 1024)
									{
										Curve_count[i] = 0;
									}
								}
							}
						}
						if (MB_SLA_RXBUF[0] != 0)
							LT_SendData_CRC_NoFrame(MB_SLA_RXBUF, 6);
					}
					else if (gMB_CMDRX.Addr >= VAR_CURVE_CS && gMB_CMDRX.Addr <= VAR_CURVE_CE)
					{
						temp = gMB_CMDRX.Addr & 0xFFF;
						for (i = 0; i < CURVE_SIZE; i++) // Store data to corresponding channels respectively
						{
							if (temp & (1 << i))
							{
								for (j = 0; j < 1024; j++)
								{
									Curve_buff[i][j] = 0x00;
								}

								Curve_count[i] = 0;
								curve_update_flag[i] = 1;
							}
						}
						if (MB_SLA_RXBUF[0] != 0)
							LT_SendData_CRC_NoFrame(MB_SLA_RXBUF, 6);
					}
					else
					{
						Feedback_ModbusBuf[0] = Addr_ModBus_Sla;
						Feedback_ModbusBuf[1] = gMB_CMDRX.CMD + 0x80;
						Feedback_ModbusBuf[2] = MB_ERR_ADDR;
						if (MB_SLA_RXBUF[0] != 0)
							LT_SendData_CRC_NoFrame(Feedback_ModbusBuf, 3);
					}
				}
				else
				{
					Feedback_ModbusBuf[0] = Addr_ModBus_Sla;
					Feedback_ModbusBuf[1] = gMB_CMDRX.CMD + 0x80;
					Feedback_ModbusBuf[2] = MB_ERR_CMD;
					if (MB_SLA_RXBUF[0] != 0)
						LT_SendData_CRC_NoFrame(Feedback_ModbusBuf, 3);
				}
			}
			else
			{
			}
		}
		else // The sending and receiving nodes are inconsistent
		{
		}
	}

	if (pit_1ms_count2 >= 10)
	{
		pit_1ms_count2 = 0;

		if (Var_Count_num > 0)
			Var_Count_flag = 1;
		if(Pointer_Count_num >0)
			Pointer_Count_flag = 1;
		if (Gif_num > 0)
			Gif_flag = 1;
		if (Adj_loop > 0)
			Adj_time++;
		if (DioKb_flag == 1)
			Cusor_time++;
		if (AioKb_flag == 1)
			Cusor_time++;
		if (GBKioKb_flag == 1)
			Cusor_time++;
		if (Scroll_num > 0)
			Scroll_flag = 1;
		if (curve_num > 0)
			curve_flag = 1;
		if (Backlight_flag)
			Backlight_count++;
		if (buzzer_start)
		{
			buzzer_10ms_count++;
			if (buzzer_10ms_count >= 5)
			{
				touch_buzzer_close();
				buzzer_start = 0;
				buzzer_10ms_count = 0;
			}
		}
#if (LT_TOUCH_FLAG == 1)
		if (RTP_read_Flag == 1)
		{
			RTP_time++;
			if (RTP_time > 5)
			{
				RTP_read_Flag = 0;
				RTP_time = 0;
			}
		}
#endif
		page_refreshing++;
		if(ctp_press_t < 0xFF) ctp_press_t++;

#if LT_ENCODER
	if (SW_flag4 != 2 || SW_flag4 != 3)
		key_time++;

	if (Encoder_read_Flag == 1)
	{
		Backlight_count = 0;
		if (Encoder_type == 0)
		{
			Encoder_time++;
			if (Encoder_time > 1)
			{
				if (Read_Encoder_key == 1)
				{
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
					SW_flag = 1;
				}
			}
		}
		else if (Encoder_type == 4)
		{
			Encoder_time++;
			if (Encoder_time > 1)
			{
				if (SW_flag4 == 1)
				{
					SW_flag4 = 2;
					SW_flag = 3;
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
				}
				if (Encoder_time > (Long_press * 5) && SW_flag4 != 3)
				{
					SW_flag4 = 3;
					SW_flag = 4;
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
				}
				if (Read_Encoder_key == 1)
				{
					SW_flag4 = 1;
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
				}
			}
		}
	}
	if (SW_flag4 == 1)
	{
		Encoder_time++;
		if (Encoder_time > Double_click_interval * 5) // Double-click valid time exceeded
		{
			SW_flag4 = 1;
			SW_flag = 2;
		}
	}
	if (Enc1)
	{
		Encoder_Freetime++;
		if (Encoder_Freetime > 100)
		{
			Encoder_Freetime = 0;
			knob_dir = 0;
			Enc1 = 0;
		}
	}
#endif
	}
}

#endif

#if (UARTBUS_OPTION == 2)

 uint32_t pit_1ms_count = 0;
 uint8_t uart_flag = 0;

 uint32_t pit_1ms_count2 = 0;
 uint32_t tx_10ms_cnt = 0;
 uint8_t tx_500ms_flag = 0;

 uint32_t pit_1ms_count3 = 0;
 uint8_t timeout_flag = 0;
 uint8_t MB_SLA_RXBUF[256] = {0};
 uint16_t MB_SLA_Len = 0;

void PIT1_Init(void)
{
	//PRE 0=1 1=2 2=4 3=8 4=16 5=32 6=64 7=128 ......
	//timeout period  = PRE * (PM + 1) * clocks

	PIT1->PCSR=0x0;//PIT3->U16PCSR.EN=0;//清零EN位
	PIT1->U16_PCSR.PRE = 5;         //预分频
	PIT1->PMR = 1*(SYS_CLOCK/1000)/32;//
	PIT1->U16_PCSR.OVW = 1;
	PIT1->U16_PCSR.PIE = 1;//中断使能PIE
	PIT1->U16_PCSR.RLD = 1;//重装位RLD
	PIT1->U16_PCSR.EN  = 1;//PIT使能EN

	EIC->IER |=IRQ(10); //enable pit3 interrupt
}

void PIT1_Handler(void)
{
		uint16_t CRC_MCU = 0, CRC_RX = 0;
		uint16_t i = 0;

		uint16_t  k;
		PIT1->U16_PCSR.PIF = 1;		//clear PIF interrupt flag

			pit_1ms_count++;
			pit_1ms_count2++;
			pit_1ms_count3++;

			if (pit_1ms_count >= 10 && uart_flag) // If the serial port is detected for 5ms and no data is received, the reception is completed
			{
				pit_1ms_count = 0;
				uart_flag = 0;
				gUsartRx.Flag = 1;

				MB_SLA_Len = gUsartRx.Count;
				memcpy(MB_SLA_RXBUF, gUsartRx.Buf, MB_SLA_Len);
				gUsartRx.Count = 0;

				CRC_MCU = (MB_SLA_RXBUF[MB_SLA_Len - 1] << 8) + (MB_SLA_RXBUF[MB_SLA_Len - 2] & 0x00FF);
				CRC_RX = CRC16(MB_SLA_RXBUF, MB_SLA_Len - 2);

				if (CRC_MCU == CRC_RX) // CRC check is correct
				{
					if (MB_SLA_RXBUF[0] == gUsartTx.Sla_Add) // The sending and receiving nodes are consistent
					{
						pit_1ms_count3 = 0;
						timeout_flag = 0;

						if (MB_SLA_RXBUF[1] == gUsartTx.CMD)	//CJC
						{
							if (gUsartTx.CMD == 0x01 || gUsartTx.CMD == 0x02) // Read command response
							{
								if ( (gUsartTx.Len%8==0 && gUsartTx.Len/8==MB_SLA_RXBUF[2]) || (gUsartTx.Len%8!=0 && gUsartTx.Len/8+1==MB_SLA_RXBUF[2]) ) // Judge whether the number of read bytes sent and responded is the same
								{
									uint16_t addr_temp = gUsartTx.Var_Add;
									Var_AddrRemapping(&addr_temp);

									for (i = 0; i < gUsartTx.Len; i++) // Save variables
									{
										//For debug
		//								if( MB_SLA_RXBUF[3+i/8]&(1<<(i%8)) )
		//									var[2 * (gUsartTx.Var_Add) + (gUsartTx.REG_Add%8+i)/8] |= 1<<( (gUsartTx.REG_Add+i)%8);
		//								else
		//									var[2 * (gUsartTx.Var_Add) + (gUsartTx.REG_Add%8+i)/8] &=~(1<<( (gUsartTx.REG_Add+i)%8) );

										if( (gUsartTx.REG_Add+i)%16 < 8)
										{
											if( MB_SLA_RXBUF[3+i/8]&(1<<(i%8)) )
												var[2 * (addr_temp + (gUsartTx.REG_Add%8+i)/16)+1] |= 1<<( (gUsartTx.REG_Add+i)%8);
											else
												var[2 * (addr_temp + (gUsartTx.REG_Add%8+i)/16)+1] &=~(1<<( (gUsartTx.REG_Add+i)%8) );
										}
										else
										{
											if( MB_SLA_RXBUF[3+i/8]&(1<<(i%8)) )
												var[2 * (addr_temp + (gUsartTx.REG_Add%8+i)/16)] |= 1<<( (gUsartTx.REG_Add+i)%8);
											else
												var[2 * (addr_temp + (gUsartTx.REG_Add%8+i)/16)] &=~(1<<( (gUsartTx.REG_Add+i)%8) );
										}
									}

									/*   Customization
									if(Master_mode03_flag[0] == 1)	//?????Code_????????????§???????????????????????????????
									{
										var[0x2002*2]   = Master_mode03_Var[0];
										var[0x2002*2+1] = Master_mode03_Var[1];
									}
									if(Master_mode03_flag[1] == 1)	//?????Code_????????????§???????????????????????????????
									{
										var[0x2003*2]   = Master_mode03_Var[2];
										var[0x2003*2+1] = Master_mode03_Var[3];
									}
									*/

									if (gUsartTx.Mode == 0x02) // Data clearing of parameter pointer address in mode 2
									{
										var[2 * (gUsartTx.Opt_Add)] = 0;
										var[2 * (gUsartTx.Opt_Add) + 1] = 0;
									}
									else if (gUsartTx.Mode == 0x03) // Customized Code_ The flag bit corresponding to mode 3 is cleared
									{
										Master_mode03_flag[gUsartTx.Opt_Add] = 0;
									}
									tx_repeat_cnt = 0;
									tx_repeat_flag = 0; // The response will not need to be retransmitted, and the next command will be executed
								}
								else
								{
									tx_repeat_flag = 1;
								}
							}
							else if (gUsartTx.CMD == 0x03 || gUsartTx.CMD == 0x04) // Read command response
							{
								if (gUsartTx.Len == MB_SLA_RXBUF[2] / 2) // Judge whether the number of read bytes sent and responded is the same
								{
									uint16_t addr_temp = gUsartTx.Var_Add;
									Var_AddrRemapping(&addr_temp);

									for (i = 0; i < MB_SLA_RXBUF[2] / 2; i++) // Save variables
									{
										var[2 * (addr_temp) + 2 * i] = MB_SLA_RXBUF[3 + 2 * i];
										var[2 * (addr_temp) + 2 * i + 1] = MB_SLA_RXBUF[3 + 2 * i + 1];

										if (addr_temp >= VAR_ADDR_S && addr_temp <= VAR_ADDR_E) // Register address
										{
											flag_MB_REG[addr_temp - VAR_ADDR_S + i] = 1;
										}
									}

									/*   Customization
									//Solves the problem that the new value of the variable is overwritten by the old value when the variable is read and written at the same time
									if(Master_mode03_flag[0] == 1)
									{
										var[0x2002*2]   = Master_mode03_Var[0];
										var[0x2002*2+1] = Master_mode03_Var[1];
									}
									if(Master_mode03_flag[1] == 1)
									{
										var[0x2003*2]   = Master_mode03_Var[2];
										var[0x2003*2+1] = Master_mode03_Var[3];
									}
									*/

									if (gUsartTx.Mode == 0x02) // Data clearing of parameter pointer address in mode 2
									{
										var[2 * (gUsartTx.Opt_Add)] = 0;
										var[2 * (gUsartTx.Opt_Add) + 1] = 0;
									}
									else if (gUsartTx.Mode == 0x03) // Customized Code_ The flag bit corresponding to mode 3 is cleared
									{
										Master_mode03_flag[gUsartTx.Opt_Add] = 0;
									}
									tx_repeat_cnt = 0;
									tx_repeat_flag = 0; // The response will not need to be retransmitted, and the next command will be executed
								}
								else
								{
									tx_repeat_flag = 1;
								}
							}
							else if (gUsartTx.CMD == 0x05 || gUsartTx.CMD == 0x06) // Write instruction response
							{
								if (gUsartTx.REG_Add == (MB_SLA_RXBUF[2] << 8 | MB_SLA_RXBUF[3])) // Judge whether the register address of sending and responding is the same
								{
									if (gUsartTx.Mode == 0x02) // Data clearing of parameter pointer address in mode 2
									{
										var[2 * (gUsartTx.Opt_Add)] = 0;
										var[2 * (gUsartTx.Opt_Add) + 1] = 0;
									}
									else if (gUsartTx.Mode == 0x03) // Customized Code_ The flag bit corresponding to mode 3 is cleared
									{
										Master_mode03_flag[gUsartTx.Opt_Add] = 0;
									}
									tx_repeat_cnt = 0;
									tx_repeat_flag = 0; // The response will not need to be retransmitted, and the next command will be executed
								}
								else
								{
									tx_repeat_flag = 1;
								}
							}
							else if (gUsartTx.CMD == 0x0F || gUsartTx.CMD == 0x10) // Write multiple instruction response
							{
								if (gUsartTx.REG_Add == (MB_SLA_RXBUF[2] << 8 | MB_SLA_RXBUF[3])) // Judge whether the register address of sending and responding is the same
								{
									if (gUsartTx.Mode == 0x02) // Data clearing of parameter pointer address in mode 2
									{
										var[2 * (gUsartTx.Opt_Add)] = 0;
										var[2 * (gUsartTx.Opt_Add) + 1] = 0;
									}
									else if (gUsartTx.Mode == 0x03) // Customized Code_ The flag bit corresponding to mode 3 is cleared
									{
										Master_mode03_flag[gUsartTx.Opt_Add] = 0;
									}
									tx_repeat_cnt = 0;
									tx_repeat_flag = 0; // The response will not need to be retransmitted, and the next command will be executed
								}
								else
								{
									tx_repeat_flag = 1;
								}
							}
						}
						else // Resend if the answer is wrong
						{
							tx_repeat_flag = 1;
						}
					}
					else // Resend if the sending and receiving nodes are inconsistent
					{
						tx_repeat_flag = 1;
					}
				}
				else
				{
					tx_repeat_flag = 1; // Resend if CRC check error
				}
				if (gUsartTx.Sla_Add != 0)
					tx_en_flag = 1; // After receiving a frame of unicast mode slave response, it is allowed to send flag bit enable
			}

			if (pit_1ms_count3 > gUsartTx.Timeout && timeout_flag)
			{
				pit_1ms_count3 = 0;
				timeout_flag = 0;
				tx_repeat_flag = 1;
				tx_en_flag = 1; // After receiving a frame, allow to send flag bit enable
			}

			if (pit_1ms_count2 > 10)
			{
				pit_1ms_count2 = 0;

				tx_10ms_cnt++;
				if (tx_10ms_cnt > 50)
				{

					tx_500ms_flag = 1;
				}

				if (Var_Count_num > 0)
					Var_Count_flag = 1;
				if(Pointer_Count_num >0)
					Pointer_Count_flag = 1;
				if (Gif_num > 0)
					Gif_flag = 1;
				if (Adj_loop > 0)
					Adj_time++;
				if (DioKb_flag == 1)
					Cusor_time++;
				if (AioKb_flag == 1)
					Cusor_time++;
				if (GBKioKb_flag == 1)
					Cusor_time++;
				if (Scroll_num > 0)
					Scroll_flag = 1;
				if (curve_num > 0)
					curve_flag = 1;
				if (Backlight_flag)
					Backlight_count++;
				if (buzzer_start)
				{
					buzzer_10ms_count++;
					if (buzzer_10ms_count >= 5)
					{
						touch_buzzer_close();
						buzzer_start = 0;
						buzzer_10ms_count = 0;
					}
				}
		#if (LT_TOUCH_FLAG == 1)
				if (RTP_read_Flag == 1)
				{
					RTP_time++;
					if (RTP_time > 5)
					{
						RTP_read_Flag = 0;
						RTP_time = 0;
					}
				}
		#endif
				page_refreshing++;
				if(ctp_press_t < 0xFF) ctp_press_t++;

		#if LT_ENCODER
			if (SW_flag4 != 2 || SW_flag4 != 3)
				key_time++;

			if (Encoder_read_Flag == 1)
			{
				Backlight_count = 0;
				if (Encoder_type == 0)
				{
					Encoder_time++;
					if (Encoder_time > 1)
					{
						if (Read_Encoder_key == 1)
						{
							Encoder_read_Flag = 0;
							Encoder_time = 0;
							key_time = 0;
							SW_flag = 1;
						}
					}
				}
				else if (Encoder_type == 4)
				{
					Encoder_time++;
					if (Encoder_time > 1)
					{
						if (SW_flag4 == 1)
						{
							SW_flag4 = 2;
							SW_flag = 3;
							Encoder_read_Flag = 0;
							Encoder_time = 0;
							key_time = 0;
						}
						if (Encoder_time > (Long_press * 5) && SW_flag4 != 3)
						{
							SW_flag4 = 3;
							SW_flag = 4;
							Encoder_read_Flag = 0;
							Encoder_time = 0;
							key_time = 0;
						}
						if (Read_Encoder_key == 1)
						{
							SW_flag4 = 1;
							Encoder_read_Flag = 0;
							Encoder_time = 0;
							key_time = 0;
						}
					}
				}
			}
			if (SW_flag4 == 1)
			{
				Encoder_time++;
				if (Encoder_time > Double_click_interval * 5) // Double-click valid time exceeded
				{
					SW_flag4 = 1;
					SW_flag = 2;
				}
			}
			if (Enc1)
			{
				Encoder_Freetime++;
				if (Encoder_Freetime > 100)
				{
					Encoder_Freetime = 0;
					knob_dir = 0;
					Enc1 = 0;
				}
			}
		#endif

			}
}
#endif

void PIT2_Init(uint16_t arr)
{
	//PRE 0=1 1=2 2=4 3=8 4=16 5=32 6=64 7=128 ......
	//timeout period  = PRE * (PM + 1) * clocks

    uint16_t count_pit   = 0;
    count_pit   = 6250;

    pit_config_ts pit_config;
    /* 将配置结构体初始化，防止意外 */
    PIT_GetDefault(&pit_config);
    pit_config.prescaler = PIT_Pre_1;

    pit_config.pit_cs.pdmae = Enable;
    pit_config.pit_cs.pdoze = false;
    pit_config.pit_cs.pdbg = false;
    pit_config.pit_cs.ovm = PIT_Ovm_Immediately;
    pit_config.pit_cs.pie   = Disable;  /* 使用中断 */
    pit_config.pit_cs.rld   = PIT_Rld_PMR;
    pit_config.pit_cs.en    = Disable; /* 先关闭 */
    pit_config.pmr          = count_pit;    /* 设置计时数量 */
    PIT_Init(PIT1, &pit_config);
}



// Disable timer PIT1
void LT_UsartTimeDisable(void)
{
//	PIT1->PCSR &= ~PCSR_PIE;
//	PIT1->PCSR &= ~PCSR_EN;
}

// Enable timer PIT1
void LT_UsartTimeEnable(void)
{

//	PIT1->PCSR |= PCSR_PIE;
//	PIT1->PCSR |= PCSR_EN;
}

// Set Load value for timer PIT1
void LT_UsartTimeSetCounter(uint32_t Counter)
{
//	PIT1->PMR = Counter;
//	PIT1->PCSR |= PCSR_PIF;
//	LT_UsartTimeEnable();
}
/**************************QSPI屏定时器驱动部分**************************/
void PIT0_Init(void)
{
    pit_config_ts pit_config;
    /* 将配置结构体初始化，防止意外 */
    PIT_GetDefault(&pit_config);
    pit_config.prescaler = PIT_Pre_64;	//200Mhz/64=3125Khz

    pit_config.pit_cs.pdmae = Enable;
    pit_config.pit_cs.pdoze = false;
    pit_config.pit_cs.pdbg = false;
    pit_config.pit_cs.ovm = PIT_Ovm_Immediately;
    pit_config.pit_cs.pie   = Disable;  /* 使用中断 */
    pit_config.pit_cs.rld   = PIT_Rld_PMR;
    pit_config.pmr          = DELAY_FRAME;    //9ms
    pit_config.pit_cs.en    = Disable;
    PIT_Init(PIT0, &pit_config);

	EIC->IER |=IRQ(EIC_IT_PIT0); //enable pit3 interrupt
}

void PIT0_EN(uint16_t pmr)
{
	PIT0->PMR = pmr;
	PIT0->U16_PCSR.PIE = 1;
	PIT0->U16_PCSR.EN = 1;
}

void PIT0_DIS(void)
{
	PIT0->U16_PCSR.PIE = 0;
	PIT0->U16_PCSR.EN = 0;
}

void PIT0_Handler(void)
{
	PIT0_DIS();
	PIT0->U16_PCSR.PIF = 1;

	TFT_QSPI_Quad_DMA_Porch(2,4,0,0x0061,4,(uint8_t*)&QSPI_BUFF_START);
	if(pit_uflag==0)
	{

		pit_uflag = 1;
		pit_urow = 0;
	}
	PIT3_EN(DELAY_LINE);
}

void PIT3_Init(void)
{
    pit_config_ts pit_config;
    /* 将配置结构体初始化，防止意外 */
    PIT_GetDefault(&pit_config);
    pit_config.prescaler = PIT_Pre_2;	//200Mhz/2=100Mhz

    pit_config.pit_cs.pdmae = Enable;
    pit_config.pit_cs.pdoze = false;
    pit_config.pit_cs.pdbg = false;
    pit_config.pit_cs.ovm = PIT_Ovm_Immediately;
    pit_config.pit_cs.pie   = Disable;  /* 使用中断 */
    pit_config.pit_cs.rld   = PIT_Rld_PMR;
    pit_config.pmr          = DELAY_LINE;    //50us
    pit_config.pit_cs.en    = Disable;
    PIT_Init(PIT3, &pit_config);

	EIC->IER |=IRQ(EIC_IT_PIT3); //enable pit3 interrupt
}

void PIT3_EN(uint16_t pmr)
{
	PIT3->PMR = pmr;
	PIT3->U16_PCSR.PIE = 1;
	PIT3->U16_PCSR.EN = 1;
}

void PIT3_DIS(void)
{
	PIT3->U16_PCSR.PIE = 0;
	PIT3->U16_PCSR.EN = 0;
}


void PIT3_Handler(void)
{
	PIT3_DIS();
	PIT3->U16_PCSR.PIF = 1;
	if(pit_uflag == 1)
	{
		QSPI2->U32_DMACR.TDMAE = 0;
		CS_ST77903_H;

		TFT_QSPI_Quad_DMA_Porch(2,4,0,0x0060,4,(uint8_t*)&QSPI_BUFF_START);
		PIT3_EN(DELAY_LINE);
		pit_urow++;
		if(pit_urow>=6)
		{
			pit_uflag = 2;
			pit_urow = 0;
		}
	}
	else if(pit_uflag == 2)
	{
		QSPI2->U32_DMACR.TDMAE = 0;
		CS_ST77903_H;

		TFT_QSPI_Quad_DMA_Line(2,6,0,0xDE006000, LCD_XSIZE_TFT*2,&qspi_tft_buf[(LCD_XSIZE_TFT*2)*pit_urow]);
		pit_urow++;
	}
	else if(pit_uflag == 3)
	{
		QSPI2->U32_DMACR.TDMAE = 0;
		CS_ST77903_H;

		TFT_QSPI_Quad_DMA_Porch(2,4,0,0x0060,4,(uint8_t*)&QSPI_BUFF_START);
		PIT3_EN(DELAY_LINE);
		pit_urow++;
		if(pit_urow>=6)
		{
			pit_uflag = 0;
			pit_urow = 0;
		}
	}
	else if(pit_uflag==0 && pit_urow==0)
	{
		QSPI2->U32_DMACR.TDMAE = 0;
		CS_ST77903_H;
		PIT0_EN(DELAY_FRAME);
	}
}
